package com.shareyi.fileutil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FileIo {
	
	private static Log log=LogFactory.getLog(FileIo.class);
	
	/**
	 * copy file from src path to dest path
	 * @param src
	 * @param dest
	 * @return
	 */
	protected static boolean  copyFile(File src,File dest)
	{

        FileInputStream fis=null;
        FileOutputStream fos=null;
		try {
			if(!dest.exists())
				{
				 dest.createNewFile();
				}

			fis=new FileInputStream(src);
			fos=new FileOutputStream(dest);
			byte[] temp=new byte[1024*8];
			int readSize=0;
			do{
				readSize=fis.read(temp);
				fos.write(temp, 0, readSize);
			}while(readSize==temp.length);
			temp=null;
			fis.close();
			
			fos.flush();
			fos.close();
		}catch (Exception e) {
			return false;
		}finally {
            IOUtils.closeQuietly(fis);
            IOUtils.closeQuietly(fos);
        }
        return true;
	}
	

	/**
	 * read the whole file content and return it as string
	 * @param file
	 * @return
	 */
	public static String readFileAsString(File file) {
		return readFileAsStringBuilder(file,null).toString();
	}

	/**
	 * read the whole file content and return it as string,use charSet while reading
	 * @param file
	 * @param charSet
	 * @return
	 */
	public static String readFileAsString(File file,String charSet) {
		return readFileAsStringBuilder(file,charSet).toString();
	}

	
	/**
	 * read the whole file content and return it as stringBuilder
	 * @param file
	 * @return
	 */
	public static StringBuilder readFileAsStringBuilder(File file) {
		return readFileAsStringBuilder(file,null);
	}
	
	
	/**
	 * read the whole file content and return it as string, use charSet while reading
	 * @param file
	 * @param charSet
	 * @return
	 */
	public static StringBuilder readFileAsStringBuilder(File file,String charSet) {
		
		StringBuilder builder=new StringBuilder();
		if(file.length()==0)
			return builder;
        FileInputStream inputStream=null;
        InputStreamReader reader=null;

		try {
			 inputStream=new FileInputStream(file);
			 reader=null;
			if(charSet==null)
				{
				 reader=new InputStreamReader(inputStream);
				}else
					reader=new InputStreamReader(inputStream,charSet);
		
			char[] chars=new char[1024*4];
			int length=0;
			do  {
				length=reader.read(chars);
				builder.append(new String(chars,0,length));
			}while(length==chars.length);
			reader.close();
			return builder;
		} catch (FileNotFoundException e) {
			log.error("文件不存在："+file.getAbsolutePath()+"\n");
			return builder;
		} catch (IOException e) {
			log.error(e.getMessage()+"\n");
			return  builder;
		}finally {
            IOUtils.closeQuietly(reader);
        }
    }
	
	
	
/**
 * write content to file use charSet
 * @param file
 * @param content
 * @param charset
 * @return
 */
public static boolean writeToFile(File file,String content,String charset)
{
	FileOutputStream outputStream=null;
	OutputStreamWriter writer=null;
	try {
		 outputStream=new FileOutputStream(file,false);
		 if(charset==null)
			 writer=new OutputStreamWriter(outputStream);
		 else
			 writer=new OutputStreamWriter(outputStream,charset);

		 writer.write(content);
	} catch (FileNotFoundException e) {
		return false;
	} catch (IOException e) {
		return false;
	}finally
	{
        IOUtils.closeQuietly(writer);
	}
	return true;
}


}
